package com.wobangkj.common.service.impl;

import com.wobangkj.common.model.crud.Comment;
import com.wobangkj.common.repository.CommentRepository;
import com.wobangkj.common.service.CommentService;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository repository;

    @Override
    public Object search(Map<String, Object> params, Comment data) {

        return CrudUtil.search(params, data, repository);
    }

    @Override
    public Object id(Long id) {
        return CrudUtil.getById(id, repository);
    }

    @Override
    public Object delete(Long id) {
        return CrudUtil.delete(id, repository);
    }
    @Override
    public Object update(Comment comment) {

        Comment existData = repository.findById(comment.getId()).get();
        copyNonNullProperties(comment, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(Comment comment) {
        try {
            Comment createData = repository.save(comment);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Override
    public List<Map> getListById(String id,String comment_text_class){return repository.getListById(id,comment_text_class);};

    @Override
    public List<Map> getListByParentId(long id){return repository.getListByParentId(id);};

    @Override
    public List<Map> findMy(String Id) {
        return repository.findMy(Id);
    }

    @Override
    public String getSharerId(String ContentId) {
        return repository.getSharerId(ContentId);
    }

    @Override
    public String getDynamicPublisherId(String ContentId) {
        return repository.getDynamicPublisherId(ContentId);
    }

    @Override
    public String getJobPublisherId(String ContentId) {
        return repository.getJobPublisherId(ContentId);
    }
}
